package com.svs.common.utils;

import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

public class RSAEncryptUtils {

	//如果重新获取公钥，私钥，可以到CreateSecrteKey类中获取后，重新赋值PUBLIC_KEY   PRIVATE_KEY 
	private static  String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjSEdDVEnX8xk3ui7t5RJXK/KvCZ4FfzKNlmqr3xrNGLT6WBWvHU8Mj5OnbyM2GDJSNyalSdqh1KyxXkPS5U84RjBN8tnaHNPF5V7T3dR7nv68s5aAjb0tcrroQx3lpvCidxCcHPzBUr3z7ITtrDAT2qNvkNMVmSkE2ALWxxExJQIDAQAB";
    private static  String PRIVATE_KEY = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKNIR0NUSdfzGTe6Lu3lElcr8q8JngV/Mo2WaqvfGs0YtPpYFa8dTwyPk6dvIzYYMlI3JqVJ2qHUrLFeQ9LlTzhGME3y2doc08XlXtPd1Hue/ryzloCNvS1yuuhDHeWm8KJ3EJwc/MFSvfPshO2sMBPao2+Q0xWZKQTYAtbHETElAgMBAAECgYApHnKTyg4ui9G17vUSg7VyrmaXHdzllXqtnUuPrMebDrXEtZoAV2koyJ9uctZzMPS7L5hT151vU5eJsytxWL3yDNr5PL0ceyvF7dzXmuee3Z6LGFEdgqTdwA62J947HGJcNgU6HkN2ecbWNdSpomsUHmT/zyJvRKIySVx2YQDDAQJBAPq4/YxurBwWCkhuuxBQs2cDl6SoqefgGKtc8VZXkOSBiLoIPiowT0Py1UMv5+6wqOVfTrM8teO0vWOKBDqFfg0CQQCmuB6FGESVRjdOeCPZz9KPoSrohQPiAsx7Ot/dsRC3jZ4jQu+TyEbquX/And9JRnuQPRVCz5rKZyRn9Uo2UNF5AkA82tfX1Sj7LDJUwGOxIxVYZJvTAIk02cTDGy0MuvZVMEeid8u268DhijhPNXqDCegEvGxavtqbKUxqs54xIiOlAkBQyK6hN10kJ93su/JDPJx+1E2CVdHH1itXGvQW7BF5n7mcxHZqWYVjf3cT0mu7ided0LtYoFgU812Esiq/nvGhAkBDRDldpVrcONdV3+mU5S8LnWAzBYd5DYMgrwg3hhVLqeY9sJQ8iIvYDi3wLleKfBJNy2cOYLpQacA5Izdb0eJ6";
	
	public static void main(String[] args) throws Exception{
		//生成公钥和私钥
		//加密字符串
		String message = "123123";
		System.out.println("当前公钥为:" + PUBLIC_KEY);
		System.out.println("当前私钥为:" + PRIVATE_KEY);
		String messageEn = encrypt(message);
		System.out.println(message + "\t加密后的字符串为:" + messageEn);
		String messageDe = decrypt(messageEn);
		System.out.println("还原后的字符串为:" + messageDe);
	}

	/** 
	 * RSA公钥加密 
	 *  
	 * @param str 
	 *            加密字符串
	 * @param publicKey 
	 *            公钥 
	 * @return 密文 
	 * @throws Exception 
	 *             加密过程中的异常信息 
	 */  
	public static String encrypt( String str) throws Exception{
		//base64编码的公钥
		byte[] decoded = Base64.decodeBase64(PUBLIC_KEY);
		RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
		//RSA加密
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.ENCRYPT_MODE, pubKey);
		String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
		return outStr;
	}

	/** 
	 * RSA私钥解密
	 *  
	 * @param str 
	 *            加密字符串
	 * @param privateKey 
	 *            私钥 
	 * @return 铭文
	 * @throws Exception 
	 *             解密过程中的异常信息 
	 */  
	public static String decrypt(String str) throws Exception{
		//64位解码加密后的字符串
		byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
		//base64编码的私钥
		byte[] decoded = Base64.decodeBase64(PRIVATE_KEY);  
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));  
		//RSA解密
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, priKey);
		String outStr = new String(cipher.doFinal(inputByte));
		return outStr;
	}
	
}
